﻿Imports System.Web
Imports System.Data
Imports System.Data.OleDb


Public Class article
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'get and display visitor name on page instead of allowing visitor to type in name
        If (Application("AnonCount") IsNot Nothing) Then
            lbl_postcommentname.Text = Request.AnonymousID
        End If

        If (User.Identity.IsAuthenticated) Then
            lbl_postcommentname.Text = User.Identity.Name
            tb_articlecommentemail.Text = Membership.GetUser().Email
        Else
            lbl_postcommentname.Text = Request.AnonymousID
        End If

        'on page load area of codebehind of like button add did user already like this article, then remove like button
        If IsPostBack = False Then
            Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
            OleDbConn.Open()
            Dim doesLikeOrNotSql As String = "Select * from postlikes WHERE PostID_fk=@f1 AND Visitor=@f2"
            Dim doesLikeOrNotCmd As OleDbCommand = New OleDbCommand(doesLikeOrNotSql, OleDbConn)
            doesLikeOrNotCmd.Parameters.AddWithValue("@f1", Request.Params("PostID"))
            doesLikeOrNotCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr As OleDbDataReader = doesLikeOrNotCmd.ExecuteReader()
            If (dr.HasRows()) Then
                btn_likepost.Visible = False
                lbl_like.Visible = True
            Else
                btn_likepost.Visible = True
                lbl_like.Visible = False
            End If
        End If


    End Sub

    Protected Sub btn_articlecommentadd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_articlecommentadd.Click
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        Dim SqlString As String = "Insert into postcomments (Visitor,Email,WebsiteURL,Title,Content,DatePosted,PostID_fk) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", lbl_postcommentname.Text)
        cmd.Parameters.AddWithValue("@f2", tb_articlecommentemail.Text)
        cmd.Parameters.AddWithValue("@f3", tb_articlecommenturl.Text)
        cmd.Parameters.AddWithValue("@f4", tb_articlecommenttitle.Text)
        cmd.Parameters.AddWithValue("@f5", tb_articlecommentcontent.Text)
        cmd.Parameters.AddWithValue("@f6", Date.Today)
        cmd.Parameters.AddWithValue("@f7", Request.Params("PostID"))
        OleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("confirmationcomment.aspx")
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'create db connection
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        Dim SqlString As String = "Insert into PostTagTable  (postID_fk,TagID_fk) Values (@f1,@f2)"
        OleDbConn.Open()

        'create checkboxlist to display existing tags for item
        For Each cb As ListItem In cbl_addexistingtag.Items
            If cb.Selected Then

                ' here is where we add the join to the table of tags and posts
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("PostID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        'adding new tag to item
        If tb_addnewtags.Text = " " Then Return
        Dim newTags = tb_addnewtags.Text.Split(",")
        For Each newTag In newTags
            'first add the new tag to the tag table
            Dim newTagSql As String = "Insert INTO tags(TagText) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, OleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'then find the id of the new tag
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("Select TOP 1 TagID from tags Order by TagID Desc", OleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate this last ID with Item
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, OleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("PostID"))
            newcmd.Parameters.AddWithValue("@f2", lastID)
            newcmd.ExecuteNonQuery()

        Next

    End Sub

    Protected Sub btn_likearticle_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_likepost.Click
        'create db connection
        Dim OleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("walkingweb").ConnectionString)
        OleDbConn.Open()

        Dim addLikeSql As String = "Insert Into postlikes (PostID_fk,Visitor) VALUES (@f1,@f2)"
        Dim addLikeCmd As OleDbCommand = New OleDbCommand(addLikeSql, OleDbConn)
        addLikeCmd.Parameters.AddWithValue("@f1", Request.Params("PostID"))
        addLikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addLikeCmd.ExecuteNonQuery()
        'showing how to hide the button for non-registered users
        btn_likepost.Visible = False
        'show this lable, put label contrl on page when registered visitor has liked this button so text {you like this article} -  set in properties - is displayed instead
        lbl_like.Visible = True

    End Sub

    Protected Sub existingcomments_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles existingcomments.Selecting

    End Sub

    Protected Sub getstartedarticle_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles getstartedarticle.Selecting

    End Sub
End Class